package com.nlx.notes.module.helper.flow;

import cn.hutool.core.util.StrUtil;
import com.nlx.notes.module.service.flow.FlowStatisticsManageService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class FlowHandlerFactory implements ApplicationContextAware {

    private static final Map<String,FlowStatisticsManageService> FLOW_STATISTICS_MANAGE_SERVICE_MAP = new HashMap<String,FlowStatisticsManageService>();

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        Map<String, FlowStatisticsManageService> contextBeansOfType = context.getBeansOfType(FlowStatisticsManageService.class);
        if(contextBeansOfType.isEmpty()){
            return ;
        }
        for (FlowStatisticsManageService flowStatisticsManageService : contextBeansOfType.values()) {
            FLOW_STATISTICS_MANAGE_SERVICE_MAP.put(flowStatisticsManageService.getType(), flowStatisticsManageService);
            FlowEventHelper.regAllFlowHandler(flowStatisticsManageService);
        }
    }

    public static FlowStatisticsManageService getInstance(String type){
        if(StrUtil.isEmpty(type)){
            return null;
        }
        return FLOW_STATISTICS_MANAGE_SERVICE_MAP.get(type);
    }


}

